﻿/*
  模块名称：外网菜单角色授权
  模块功能：对菜单进行选择，可以授权某角色可访问的页面和页面按钮功能，二次开发人员，请理解代码后，再进行修改；
  当前版本：2.0
  开发人员：马立涛
  开始时间：2009-03-01
  
  修改时间：
  修 改 人：
 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OutPage_Role_Edit : System.Web.UI.Page
{
    BaiMaSoft.Conn.DataConn MyDataConn = new BaiMaSoft.Conn.DataConn();
    public SqlConnection myconn;

    public SqlDataAdapter myadapter;
    public DataSet myds;
    public DataTable mytb;
    public DataColumn mycolumn;
    public string filedtotal = "";
    public string sDeptHtml = "";

    public string sHtml = "", ll = "";
    public string sPCode = "";
    public string sPName = "";
    int nBtnCount = 0; //是否有被授权使用的按钮
    public string sEditType = "1";

    RoleManage MyRM = new RoleManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["VHtml"] == null)
        {
            ViewState["VHtml"] = "";
        }
        if (!Page.IsPostBack)
        {
            try
            {
                sEditType = Request.QueryString["EditType"].ToString();
            }
            catch
            {
                sEditType = "1";
            }

            if (sEditType == "1")
            {
                RoleCode.Text = MyRM.GetMaxNumber("Sys_OutRole_Info", "Code");  //新建时,插入表中最大值自增1
            }

            string sIniOrgID = "0";
            ShowGrantTree(sIniOrgID);
        }
        sHtml = ViewState["VHtml"].ToString().Trim();
    }

    /// <summary>
    /// 展现树
    /// </summary>
    public void ShowGrantTree(string sSOrgID)
    {
        MyDataConn.Connection.Open();
        myadapter = new SqlDataAdapter(" select  ID, ParentID,ChildNumber, Label as Name,IFLink,Link, Layer,IfDbGrant,Dorder,Effective from Sys_OutPage_Info ORDER BY ParentID, DOrder ", MyDataConn.Connection);
        myds = new DataSet();
        myadapter.Fill(myds, "Sys_OutPage_Info");
        mytb = myds.Tables["Sys_OutPage_Info"];

        foreach (DataColumn mycolumn in mytb.Columns)
        {
            filedtotal = filedtotal + mycolumn.ColumnName + ",";
        }

        sDeptHtml = sDeptHtml + "<script language=\"javascript\" type=\"text/javascript\">";
        filedtotal = filedtotal.Substring(0, filedtotal.Length - 1);
        sDeptHtml = sDeptHtml + " function tree(" + filedtotal + "){";
        string[] sfiledtotal = filedtotal.Split(',');
        int nfiledtotal = sfiledtotal.Length;
        for (int i = 0; i < nfiledtotal; i++)
        {
            sDeptHtml = sDeptHtml + "this." + sfiledtotal[i] + "=" + sfiledtotal[i] + ";";
        }

        if (mytb.Rows.Count > 0)
        {
            int iRowCount = mytb.Rows.Count - 1;
            sDeptHtml = sDeptHtml + "}var trees =new Array(" + iRowCount + ");";
            PCount.Value = iRowCount.ToString();
        }
        else
        {
            sDeptHtml = sDeptHtml + "}var trees =new Array(0);";
            PCount.Value = "0";
        }

        for (int j = 0; j < mytb.Rows.Count; j++)
        {
            sDeptHtml = sDeptHtml + "trees[" + j + "] = new tree(";
            string temp = "";
            for (int k = 0; k < mytb.Columns.Count; k++)
            {
                temp = temp + "'" + mytb.Rows[j][k] + "'";
                if (k != mytb.Columns.Count - 1)
                {
                    temp = temp + ",";
                }
            }
            sDeptHtml = sDeptHtml + temp + ");";
        }
        sDeptHtml = sDeptHtml + "showtree(0,'0');";
        sDeptHtml = sDeptHtml + "</script>";
        sDeptHtml = sDeptHtml + "";
        MyDataConn.Connection.Close();
    }

    /// <summary>
    /// 保存授权信息
    /// </summary>
    /// <param name="sCode"></param>
    /// <returns></returns>
    public int SaveGrantInfo(string sCode)
    {
        string pID = hidPageIDList.Value;
        string sPageBtnTotal = "";
        if (pID != "" && pID != null)
        {
            int iback = MyRM.InsertOutRoleGrantInfo (sCode, pID);
            if (iback == 1)
            {
                return 1; //授权成功
            }
            else
            {
                return 0; //授权失败
            }
        }
        else
        {
            return 2; //没有授权信息
        }
    }

    /// <summary>
    /// 授权按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SaveInfo_Click(object sender, EventArgs e)
    {
        string sUserRole = Session["Baimasoft_Role_Code"].ToString().Trim();
        //保存角色信息
        string sRoleCode = RoleCode.Text.ToString().Trim();
        string sRoleName = RoleName.Text.ToString().Trim();
        string sRoleDescr = RoleDescr.Text.ToString().Trim();
        string sEditType = hidEditType.Value.ToString().Trim();
        string sEffective = Effective.SelectedValue.ToString().Trim();
        string sUserID = "";// HChoiceID_1.Value.ToString().Trim();   //把角色分配给E用户ID

        if ((sRoleCode == "" || sRoleCode == null) && (sRoleName == "" || sRoleName == null))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色代码、角色名称不能为空!');</script>");
            return;
        }
        else if (sRoleCode == "" || sRoleCode == null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色代码不能为空!');</script>");
            return;
        }
        else if (sRoleName == "" || sRoleName == null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称不能为空!');</script>");
            return;
        }

        if (sRoleDescr.Length > 200)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色描述不能超过200个字!');</script>");
            return;
        }

        int iBackCode = MyRM.JudgeOutRoleCode (sRoleCode).Rows.Count;  //判断角色代码是否重复
        int iBackName = MyRM.JudgeOutRoleCode (sRoleName).Rows.Count;  //判断角色名称是否重复

        if (sEditType == "1") // 新建
        {
            if (iBackCode == 1 && iBackName == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色代码、角色名称已创建!');</script>");
                return;
            }
            else if (iBackCode == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色代码已创建!');</script>");
                return;
            }
            else if (iBackName == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称已创建!');</script>");
                return;
            }

            MyRM.DelOutRoleGrantInfo (sRoleCode);        //删除相关授权信息
            int iBackGrant = SaveGrantInfo(sRoleCode);   //保存授权信息

            if (iBackGrant == 2)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择授权信息!');</script>");
                //return;
            }

            int iBackSave = MyRM .InsertOutRoleInfo (sRoleCode, sRoleName, sRoleDescr, sUserRole, sEffective, sUserID); //保存角色信息

            if (iBackGrant == 1 && iBackSave == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建成功!');window.close();</script>");
                hidPageIDList.Value = "";  //清空页面授权信息
                hidBtnIDList.Value = "";  //清空按钮授权信息
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建失败!');</script>");
                MyRM.DelRoleInfo(sRoleCode);
                MyRM.DelRoleGrantInfo(sRoleCode);
                return;
            }
        }
        else if (sEditType == "2") //修改
        {
            string sPCode = OrRoleCode.Value.ToString().Trim();
            string sPName = OrRoleName.Value.ToString().Trim();
            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sPCode + sPName + "');</script>");
            if (sPCode.Trim() != sRoleCode.Trim() && iBackCode == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色代码已创建!');</script>");
                return;
            }

            if (sPName.Trim() != sRoleName.Trim() && iBackName == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('角色名称已创建!');</script>");
                return;
            }

            MyRM.DelOutRoleGrantInfo (sRoleCode);      //删除相关授权信息     
            int iBackGrant = SaveGrantInfo(sRoleCode);  //保存授权信息   

            if (iBackGrant == 2)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择授权信息!');</script>");
                return;
            }

            int iBackSave = MyRM.UpdateOutRoleInfo (sRoleCode, sRoleName, sRoleDescr, sUserRole, sPCode, sEffective, sUserID); //修改角色信息
            if (iBackGrant == 1 && iBackSave == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');;window.close();</script>");
                hidPageIDList.Value = "";  //清空页面授权信息
                hidBtnIDList.Value = "";  //清空按钮授权信息
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }
    }
}
